<template>
  <div class="dataset-bg">
    <el-row :gutter="0" style="height: 100%;">
      <!-- 左侧部门树 -->
      <el-col :span="5" :xs="24" style="height: 100%;">
        <div class="left-container">
          <DeptTree @node-click="handleDeptNodeClick" type="dataset" style="height: 100%;" />
        </div>
      </el-col>
      <el-col :span="19" :xs="24">
        <div class="right-container">
          <el-form
            class="-mb-15px gw-flex-between dataset-form"
            :model="queryParams"
            ref="queryFormRef"
            :inline="true"
            label-width="68px"
          >
          <div>
            <el-form-item label="数据集名称" prop="name" label-width="90px">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
                class="!w-240px"
              />
            </el-form-item>
            <el-form-item label="数据量" prop="num">
              <el-input
                v-model="queryParams.num"
                placeholder="请输入"
                clearable
                @keyup.enter="handleQuery"
                class="!w-240px"
              />
            </el-form-item>
          </div>
            <el-form-item>
              <el-button @click="resetQuery" plain type="primary">重置</el-button>
              <el-button @click="handleQuery" type="primary">查询</el-button>
              <el-button
                type="primary"
                class="btn-ml"
                @click="openForm('create')"
                v-hasPermi="['multiModel:dataset:create']"
                v-if="isLeafNode"
              >
                新增数据集
              </el-button>
            </el-form-item>
          </el-form>
          <div class="table-content">
            <el-table :data="list" :stripe="true" :max-height="tableMaxHeight" :show-overflow-tooltip="true" :header-cell-style="{ 'background': '#CCEBE7', 'color': '#585858' }" :align="'center'">
              <el-table-column label="序号" type="index" width="60px" align="center" />
              <el-table-column label="数据集名称" prop="name" align="center" />          
              <el-table-column label="数据集类型" prop="datasetTypeName" align="center" />
              <el-table-column label="数据量" prop="num" align="center" />
              <el-table-column label="描述" prop="remark" align="center" />
              <el-table-column label="操作" fixed="right" align="center" width="220px">
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    @click="openForm('view', scope.row.id)"
                    v-hasPermi="['multiModel:dataset:query']"
                  >
                    查看
                  </el-button>
                  <el-button
                    link
                    type="primary"
                    @click="openForm('update', scope.row.id)"
                    v-hasPermi="['multiModel:dataset:update']"
                  >
                    编辑
                  </el-button>
                  <el-button
                    link
                    type="primary"
                    @click="handleDetail(scope.row.id)"
                    v-hasPermi="['multiModel:dataset:query']"
                  >
                    详情
                  </el-button>
                  <el-button
                    link
                    type="danger"
                    @click="handleDelete(scope.row.id)"
                    v-hasPermi="['multiModel:dataset:delete']"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <Pagination
              class="mr-10px"
              :total="total"
              v-model:page="queryParams.pageNo"
              v-model:limit="queryParams.pageSize"
              @pagination="getList"
            />
          </div>
        </div>
        <!-- 表单弹窗：添加/修改 -->
        <DatasetForm ref="formRef" @success="getList" />
        <DelConfirmDialog ref="delConfirmRef" @confirm="deleteConfirm" />
      </el-col>
    </el-row>
  </div>
  
</template>
<script lang="ts" setup>
import DatasetForm from './DatasetForm.vue'
import { DatasetApi, DatasetVO } from '@/api/multiModel/dataset'

/** 数据集 列表 */
defineOptions({ name: 'EvaluationDataset' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const list = ref<DatasetVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  createTime: [],
  datasetTypeId: undefined,
  num: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  try {
    let data = await DatasetApi.getDatasetPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id || 0, queryParams.datasetTypeId, datasetTypeName.value)
}
/** 详情 */
const router = useRouter() // 路由
const handleDetail = (id: number) => {
  router.push({
    path: '/evaluationDataset/detail',
    query: {
      id
    }
  })
}

/** 删除按钮操作 */
const delConfirmRef = ref()
const handleDelete = async (id: number) => {
  delConfirmRef.value.open(id, ['是否确定删除选中评测数据集?'])
}
/** 删除按钮操作 */
const deleteConfirm = async (id: number) => {
  try {
    await DatasetApi.deleteDataset(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const datasetTypeName = ref('')
const isLeafNode = ref(false)
/** 数据集节点被点击 */
const handleDeptNodeClick = async (id, name, isLeaf) => {
  isLeafNode.value = isLeaf
  queryParams.datasetTypeId = id
  datasetTypeName.value = name
  await getList()
}

const tableMaxHeight = ref('300px'); // 初始高度
// 表格高度
const updateMaxHeight = () => {
  tableMaxHeight.value = document.body.clientHeight - 385 + 'px'
}
window.addEventListener('resize', updateMaxHeight)
/** 初始化 **/
onMounted(() => {
  getList()
  updateMaxHeight()
})
onUnmounted(() => {
  window.removeEventListener('resize', updateMaxHeight)
})
</script>
<style scoped lang="scss">
.dataset-bg {
  background-color: #fff;
  padding: 16px 32px;
  height: calc(100% - 90px);
}
.left-container {
  height: 100%;
}
.right-container {
  height: 100%;
  border: 1px solid #CDD7D5;
  border-left: 0;
}
.dataset-form {
  display: flex;
  padding: 16px;
  flex-wrap: wrap;
  .el-form-item:last-child {
    margin-right: 0px !important;
  }
}
.table-content {
  border-top: 1px solid #CDD7D5;
  padding-top: 16px;
  padding-left: 16px;
  padding-right: 16px;
}
.btn-group {
  flex: 1;
  min-width: 280px;
  display: flex;
  justify-content: flex-end;
}
</style>
